package com.loongtech.bi.manager.business;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.pay.EntityFirstPayUser;
import com.loongtech.bi.manager.pay.TotalCashManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ReconciliationBusinessManager extends ManagerQueryCacheBase<EntityFirstPayUser> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private TotalCashManager totalCashManager;
	@Autowired
	private AddcashBalanceManager addcashBalanceManager;

	//获得汇总数据
	public List<BusinessData> getBusinessSumData(Date dateStart, Date dateEnd){
		//ios
		String iosPingTai = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.IOSPINGTAI).keySet().toArray()[1];
		//android
		String androidPingTai = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.ANDROIDPINGTAI).keySet().toArray()[1];
		//web 直充
		String webzhichong = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.WEBZHICHONG).keySet().toArray()[1];
		//游戏内充值
		String youxineichongzhi = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.YOUXINEICHONGZHI).keySet().toArray()[1];
		
		List<BusinessData> result = new ArrayList<BusinessData>();
		BusinessData businessSumData = new BusinessData();
		//充值list
		List<Object[]> totalCashList = totalCashManager.getSumTotalCashByAllKind(dateStart, dateEnd, "0", "0", "0");
		businessSumData.setAddcash(UIHelper.getYuan(NumberUtil.parseLong(totalCashList.get(0))));
		//ios
		List<Object[]> iosCashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, iosPingTai, "0", "0", youxineichongzhi);
		businessSumData.setIosAddcash(UIHelper.getYuan(NumberUtil.parseLong(iosCashList.get(0))));
		//android
		List<Object[]> androidCashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, androidPingTai, "0", "0", youxineichongzhi);
		businessSumData.setAndroidAddcash(UIHelper.getYuan(NumberUtil.parseLong(androidCashList.get(0))));
		//web 直充
		List<Object[]> webAddcashList = addcashBalanceManager.getSumAddcashByAllKindByAddtype(dateStart, dateEnd, "0", "0", "0", webzhichong);
		businessSumData.setWebAddcash(UIHelper.getYuan(NumberUtil.parseLong(webAddcashList.get(0))));
		
		//订单数总和
		List<Object[]> totalOrderNumList = addcashBalanceManager.getSumOrderNumByAllKind(dateStart, dateEnd, "0", "0", "0");
		businessSumData.setOdernum(NumberUtil.parseInt(totalOrderNumList.get(0)));
		//ios 订单
		List<Object[]> iosOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, iosPingTai, "0", "0",youxineichongzhi);
		businessSumData.setIosOdernum(NumberUtil.parseInt(iosOdernumList.get(0)));
		//android 订单
		List<Object[]> androidOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, androidPingTai, "0", "0",youxineichongzhi);
		businessSumData.setAndroidOdernum(NumberUtil.parseInt(androidOdernumList.get(0)));
		//web 订单
		List<Object[]> webOdernumList = addcashBalanceManager.getSumOrderNumByAllKindByAddtype(dateStart, dateEnd, "0", "0", "0",webzhichong);
		businessSumData.setWebOdernum(NumberUtil.parseInt(webOdernumList.get(0)));
		
		//加入
		result.add(businessSumData);
		return result;
	}
	
	//获得详细数据
	public List<BusinessData> getBusinessDetailData(Date dateStart, Date dateEnd){
		//ios
		String iosPingTai = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.IOSPINGTAI).keySet().toArray()[1];
		//android
		String androidPingTai = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.ANDROIDPINGTAI).keySet().toArray()[1];
		//web 直充
		String webzhichong = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.WEBZHICHONG).keySet().toArray()[1];
		//游戏内充值
		String youxineichongzhi = (String) GameParamConfigManager.getItemsRevise(CommonGameParam.YOUXINEICHONGZHI).keySet().toArray()[1];
				
		List<BusinessData> result = new ArrayList<BusinessData>();
		//map
		Map<String,BusinessData> map = new HashMap<String,BusinessData>();
		//all 充值
		List<Object[]> addcashList = totalCashManager.getTotalCashByAllKind(dateStart, dateEnd, "0", "0", "0");
		for(Object[] o : addcashList) {
			BusinessData businessData = new BusinessData();
			businessData.setDay(o[0].toString());
			businessData.setAddcash(businessData.getAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
			
			//map 存入
			map.put(o[0].toString(), businessData);
		}
		
		//ios充值
		List<Object[]> iosAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, iosPingTai, "0", "0",youxineichongzhi);
		for(Object[] o : iosAddcashList) {
		    if (o == null || o.length <=0 || o[0] == null) {
		        continue;
            }
			BusinessData businessData = map.get(o[0].toString());
			if(businessData != null) {
				businessData.setIosAddcash(businessData.getIosAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//android充值
		List<Object[]> androidAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, androidPingTai, "0", "0",youxineichongzhi);
		for(Object[] o : androidAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setAndroidAddcash(businessData.getAndroidAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//web充值
		List<Object[]> webAddcashList = addcashBalanceManager.getAddcashByAllKindByAddtypeByday(dateStart, dateEnd, "0", "0", "0",webzhichong);
		for(Object[] o : webAddcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setWebAddcash(businessData.getWebAddcash() + UIHelper.getYuan(NumberUtil.parseLong(o[1])));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//odernum
		List<Object[]> odernumList = addcashBalanceManager.getOrderNumByAllKindByDay(dateStart, dateEnd, "0", "0", "0");
		for(Object[] o : odernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setOdernum(businessData.getOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//iosOdernum
		List<Object[]> iosOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, iosPingTai, "0", "0",youxineichongzhi);
		for(Object[] o : iosOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setIosOdernum(businessData.getIosOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//androidOdernum
		List<Object[]> androidOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, androidPingTai, "0", "0",youxineichongzhi);
		for(Object[] o : androidOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setAndroidOdernum(businessData.getAndroidOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//webOdernum
		List<Object[]> webOdernumList = addcashBalanceManager.getOrderNumByAllKindByAddtypeByDay(dateStart, dateEnd, "0", "0", "0",webzhichong);
		for(Object[] o : webOdernumList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				businessData.setWebOdernum(businessData.getWebOdernum() + NumberUtil.parseInt(o[1]));
				
				//map 存入
				map.put(o[0].toString(), businessData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : addcashList) {
            if (o == null || o.length <=0 || o[0] == null) {
                continue;
            }

			BusinessData businessData = map.get(o[0].toString());
			
			if(businessData != null) {
				result.add(businessData);
			}
		}
		
		return result;
	}
	
	//对账汇总数据
	public class BusinessData {
		private String day;
		private long addcash; //充值
		private long iosAddcash; //苹果游戏内充值
		private long androidAddcash; //安卓游戏内充值
		private long webAddcash; //web直充
		private int odernum; //订单号
		private int iosOdernum; //苹果游戏内订单号
		private int androidOdernum; //安卓游戏内订单号
		private int webOdernum; //web直充订单号
		public long getAddcash() {
			return addcash;
		}
		public void setAddcash(long addcash) {
			this.addcash = addcash;
		}
		public long getIosAddcash() {
			return iosAddcash;
		}
		public void setIosAddcash(long iosAddcash) {
			this.iosAddcash = iosAddcash;
		}
		public long getAndroidAddcash() {
			return androidAddcash;
		}
		public void setAndroidAddcash(long androidAddcash) {
			this.androidAddcash = androidAddcash;
		}
		public long getWebAddcash() {
			return webAddcash;
		}
		public void setWebAddcash(long webAddcash) {
			this.webAddcash = webAddcash;
		}
		public int getOdernum() {
			return odernum;
		}
		public void setOdernum(int odernum) {
			this.odernum = odernum;
		}
		public int getIosOdernum() {
			return iosOdernum;
		}
		public void setIosOdernum(int iosOdernum) {
			this.iosOdernum = iosOdernum;
		}
		public int getAndroidOdernum() {
			return androidOdernum;
		}
		public void setAndroidOdernum(int androidOdernum) {
			this.androidOdernum = androidOdernum;
		}
		public int getWebOdernum() {
			return webOdernum;
		}
		public void setWebOdernum(int webOdernum) {
			this.webOdernum = webOdernum;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
	}
}
